#include "StringHelper.h"


CStringHelper::CStringHelper()
{
}


CStringHelper::~CStringHelper()
{
}

INT CStringHelper::Split(const KIV_STR& str, vector<KIV_STR>& ret_, KIV_STR sep /*= ","*/)
{
	if (str.empty())
		return 0;

	KIV_STR tmp;
	KIV_STR::size_type pos_begin = str.find_first_not_of(sep);
	KIV_STR::size_type comma_pos = 0;

	while (pos_begin != string::npos)
	{
		comma_pos = str.find(sep, pos_begin);
		if (comma_pos != string::npos)
		{
			tmp = str.substr(pos_begin, comma_pos - pos_begin);
			pos_begin = comma_pos + sep.length();
		}
		else
		{
			tmp = str.substr(pos_begin);
			pos_begin = comma_pos;
		}

		if (!tmp.empty())
		{
			ret_.push_back(tmp);
			tmp.clear();
		}
	}
	return 0;
}

KIV_STR CStringHelper::Replace(const KIV_STR& str, const KIV_STR& src, const KIV_STR& dest)
{
	KIV_STR ret;

	KIV_STR::size_type pos_begin = 0;
	KIV_STR::size_type pos = str.find(src);
	while (pos != string::npos)
	{
		ret.append(str.data() + pos_begin, pos - pos_begin);
		ret += dest;
		pos_begin = pos + 1;
		pos = str.find(src, pos_begin);
	}
	if (pos_begin < str.length())
	{
		ret.append(str.begin() + pos_begin, str.end());
	}
	return ret;
}
